using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.Presenters.Views;
using CMS.Presenters.Presenters;
using System.Collections.Generic;
using CMS.Data;

namespace CMS.Web.Template
{
    public partial class ThongBao : ViewUserControl, IThongBaoView
    {
        ThongBaoPresenter presenter;
        IList<CMS.Core.Domain.ThongBao> objIList;

        public ThongBao()
        {
            this.presenter = new ThongBaoPresenter(this, NHibernateDaoFactory.Instance.GetThongBaoDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            presenter.LoadViewFromModel();
            if (objIList.Count == 1)
            {
                this.trDetail.Visible = true;
                this.ViewDetail(objIList[0]);
            }
            else
            {
                if (!this.IsPostBack)
                {
                    this.gridObj.DataSource = this.objIList;
                    this.gridObj.DataBind();
                }
                if (this.VId > 0)
                {
                    CMS.Core.Domain.ThongBao objView = NHibernateDaoFactory.Instance.GetThongBaoDao().GetById(this.VId);
                    this.trDetail.Visible = true;
                    this.ViewDetail(objView);
                }
                else
                {
                    this.trDetail.Visible = false;
                }
            }
        }

        private void ViewDetail(CMS.Core.Domain.ThongBao thongbao)
        {

            this.lblTitle.Text = thongbao.TenThongBao;
            this.lblContent.Text = thongbao.NoiDung;
            if (!thongbao.FileDinhKem.Equals(""))
            {
                this.trFile.Visible = true;
                this.TenFile.Attributes.Add("href", "~/sharedoc/thongbao/" + thongbao.FileDinhKem);
            }
            else
            {
                this.trFile.Visible = false;
                this.TenFile.Attributes.Add("href", "#");
            }
        }

        protected void gridObj_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gridObj.DataSource = this.objIList;
            gridObj.PageIndex = e.NewPageIndex;
            gridObj.DataBind();
        }

        protected void gridObj_RowCreated(object sender, GridViewRowEventArgs e)
        {
            CMS.Core.Domain.ThongBao thongbao = (CMS.Core.Domain.ThongBao)e.Row.DataItem;
            if (thongbao != null)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    foreach (Control c in e.Row.Cells[0].Controls)
                    {
                        LinkButton lblTenThongBao = (LinkButton) c.FindControl("lblTenThongBao");

                        if (lblTenThongBao != null)
                        {
                            lblTenThongBao.Text = thongbao.TenThongBao.ToString();
                            lblTenThongBao.Attributes.Add("onclick", "return doView(\"" + this.BaseUrl + "?aid=1&vid=" + thongbao.ID.ToString() + "\");");
                        }
                    }
                }
            }

            if (e.Row.RowType == DataControlRowType.Pager)
            {
                // Disabled control 
                foreach (GridViewRow row in gridObj.Rows)
                {
                    CMS.Core.Domain.ThongBao obj = ThongBaoDao.Instance.GetById(Convert.ToInt32(gridObj.DataKeys[row.RowIndex].Value.ToString()));
                }

                foreach (Control c in e.Row.Cells[0].Controls)
                {
                    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.PagerTable"))
                    {
                        Table tbl = (Table)c;
                        TableCell cell = new TableCell();
                        cell.Text = "Trang ";
                        cell.HorizontalAlign = HorizontalAlign.Right;
                        tbl.Rows[0].Cells.AddAt(0, cell);
                    }
                }
            }
        }

        public string ShowFileDinhKem(string fileName)
        {
            return (!fileName.Equals("")) ? "" : "none";
        }

        protected void lbnTitle_Click(object sender, EventArgs e)
        {
            int maThongBao = this.VId;
        }

        #region IThongBaoView Members

        IList<CMS.Core.Domain.ThongBao> IThongBaoView.ThongBaoList
        {
            get { return this.objIList; }
            set { this.objIList = value; }
        }

        public bool IsValid
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        #endregion
    }
}